package com.e3.bill.client;

import com.e3.web.core.model.SuperModel;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyButton;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;
/**
 * 卡片模式下封装BillGridPanel，增加行操作。
 * Description: <br>
 * @author: <a href="mailto:senton1101@126.com">TianXiangdong</a> 2010-9-23
 */
public class BillCardGridPanel<D extends SuperModel> extends LayoutContainer {
	
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	private BillGridPanel<D> billGridPanel;
	
	public BillCardGridPanel(BillGridPanel<D> billGridPanel){
		this.billGridPanel = billGridPanel;
	}
	
	public BillGridPanel<D> getBillGridPanel() {
		return billGridPanel;
	}

	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new FitLayout());
		
		ContentPanel contentPanel = new ContentPanel(new BorderLayout());
		initContentPanel(contentPanel);
		
		ToolBar toobar = new ToolBar();
		initToolBar(toobar);
		
		contentPanel.setTopComponent(toobar);
		add(contentPanel);
	}


	protected void initContentPanel(ContentPanel contentPanel){
		contentPanel.setHeaderVisible(false);
		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.CENTER);
		contentPanel.add(billGridPanel,bld);
	}

	protected void initToolBar(ToolBar toolbar){
		MyButton[] buttons = getToolButtons();
		if(buttons!=null){
			for (MyButton myButton : buttons) {
				toolbar.add(myButton);
			}
		}
	}
	
	protected MyButton[] getToolButtons(){
		BaseAction[] actions = {BillStateMachine.ACTION_ROW_ADD,BillStateMachine.ACTION_ROW_INSERT
				,BillStateMachine.ACTION_ROW_DEL,BillStateMachine.ACTION_ROW_COPY
				,BillStateMachine.ACTION_ROW_PASTE};
		MyButton[] buttons = new MyButton[actions.length];
		for (int i=0;i<actions.length;i++) {
			MyButton button = new MyButton(actions[i]);
			button.addSelectionListener(btnSelectListener);
			buttons[i] = button;
		}
		return buttons;
	}
	
	protected void onBtnMnuEvent(BaseAction action) {
		if(action == BillStateMachine.ACTION_ROW_ADD){
			billGridPanel.addGridValue(billGridPanel.createModelInstance());
		}
		else if(action == BillStateMachine.ACTION_ROW_INSERT){
			
		}
		else if(action == BillStateMachine.ACTION_ROW_DEL){
					
		}
		else if(action == BillStateMachine.ACTION_ROW_COPY){
			
		}
		else if(action == BillStateMachine.ACTION_ROW_PASTE){
			
		}
	}
	
	
}
